package com.tjbklx1.pattern2.chap34;

public class Invoker {
	// 执行命令
	public String exec(String _commandStr) throws Exception {
		// 定义返回值
		String result = "";
		// 首先解析命令
		CommandVO vo = new CommandVO(_commandStr);
		// 检查是否支持该命令
		boolean validFlag = CommandEnum.getNames().contains(vo.getCommandName());
		if (validFlag) {
			// 产生命令对象
			String className = CommandEnum.valueOf(vo.getCommandName()).getValue();
			Command command = null;
			try {
				command = (Command) Class.forName(className).newInstance();
			} catch (Exception e) {
				// TODO 异常处理
			}
			if (command == null) {
				System.out.println("没有发现这个class :" + className);
				throw new Exception("没有发现这个class :" + className);
			}
			result = command.execute(vo);
		} else {
			result = "无法执行命令，请检查命令格式";
		}
		return result;
	}
}